package com.ruantu.clothing.controller;

import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.service.GongyingshangService;
import com.ruantu.clothing.service.TailorService;
import com.ruantu.clothing.util.MyException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/6/7 0007.
 */
@Controller
@RequestMapping("/tailor")
public class TailorController {
    private static final Logger logger = LoggerFactory.getLogger(TailorController.class);
    @Autowired
    private TailorService tailorService;

    /**
     * 查询所有裁剪单
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findList(Tailor tailor, PageBean pageBean) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(tailorService.findList(tailor, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }

    /**
     * 根据裁剪单id查询子信息
     *
     * @return
     */
    @RequestMapping("/childrenlist/id")
    @ResponseBody
    public JsonResponseMessage findListChildren(String id, int fenlei) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(tailorService.findListById(id, fenlei));
        return jsonResponseMessage;
    }

    /**
     * 查询贴牌
     *
     * @return
     */
    @RequestMapping("/tiepai")
    @ResponseBody
    public List<Tailor> findComboboxTiepai() {
        return tailorService.findTiepai();
    }

    /**
     * 删除裁剪单
     *
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public JsonResponseMessage del(Tailor tailor) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            tailorService.delTailor(tailor);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setMsg("删除失败，服务器内部错误");
        }
        return jsonResponseMessage;
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResponseMessage save(String a, Tailor tailor, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        tailor.setZhidanren(user.getId());
        tailor.setUpdateBy(user.getId());
        try {
            tailorService.save(a, tailor);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请刷新重新尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 修改返显
     *
     * @param tailor
     * @return
     */
    @RequestMapping("/find/id")
    @ResponseBody
    public JsonResponseMessage findById(Tailor tailor) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            jsonResponseMessage.setData(tailorService.findById(tailor));
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 修改成本价
     *
     * @param tailor
     * @return
     */
    @RequestMapping("/save/chengbenjia")
    @ResponseBody
    public JsonResponseMessage saveChengbenjia(Tailor tailor) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            jsonResponseMessage.setData(tailorService.saveChengbenjia(tailor));
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 结束裁剪单，已全入库
     *
     * @param tailor
     * @return
     */
    @RequestMapping("/over")
    @ResponseBody
    public JsonResponseMessage overCaijiandan(Tailor tailor) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            jsonResponseMessage.setData(tailorService.overCaijiandan(tailor));
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }

    @RequestMapping("/combobox/overtailor")
    @ResponseBody
    public List<Tailor> findOverTailor(String q) {
        return tailorService.findOverTailor(q);
    }


}
